#include"Playlist.h" 

#include"Song.h"
#include"StatusBar.h"

#include<QIcon>

Playlist::Playlist(){
	m_index = -1;
	m_trackProgression = TrackProgression::getTrackProgression(&m_songs, TrackProgression::Default, TrackProgression::NoRepeat);
}

Playlist::Playlist(Playlist *other){
// 	throw PlaylistException("Playlist::Playlist(Playlist *other) not yet properly implemented.");
	while(other->m_songs.size())
		add( other->m_songs.takeAt(0) );
	other->clear();
}

Playlist::~Playlist(){
	delete m_trackProgression;
	while(m_songs.size())
		delete m_songs.takeAt(0);
}

bool Playlist::isEmpty()const{
	return m_songs.size() == 0;
}

bool Playlist::previous(){
	m_trackProgression->previous();
	if(m_index > 0)
		m_index--;
	return true;
}

bool Playlist::next(){
	bool ret = m_trackProgression->next();
	/*if(m_index >= 0)
		m_index++;
	else
		m_index = 0;
	
	if(m_index >= m_songs.size())
		return false;*/
	return ret;
}

void Playlist::setIndex(short int index){
	m_trackProgression->setIndex(index);
	m_index = index;
}

QPointer<Song> Playlist::current()const{
	short int index = m_trackProgression->current();
	if(index >= 0 && index < m_songs.size()){
		emit currentChanged(index);
		emit nowPlaying(m_songs[index]);
		return m_songs[index];
	}else
		return 0;
}

void Playlist::clear(){
	while(m_songs.size())
		delete m_songs.takeAt(0);
	m_trackProgression->cleared();
	_clear();
	emit cleared();
}

void Playlist::add(QPointer<Song> song, short int index){
	if(index < 0 || index >= m_songs.size())
		m_songs.push_back(song);
	else
		m_songs.insert(index, song);
	m_trackProgression->songAdded(song);
	_addSong(song,index);
	emit songAdded(song, index);
}

void Playlist::add(const QString &songPath, short int index){
	try{
		QPointer<Song> song = new Song(songPath);
		add(song,index);
	}catch(const SongException &e){
		sStatusBar->log(QString("Song at ") + songPath + " could not be loaded or does not exist.");
	}
}

void Playlist::add(QList<QString> songs){
	for(int i = 0; i < songs.size(); i++)
		add(songs[i]);
}

TrackProgression::ProgressionType Playlist::progressionType()const{
	return m_trackProgression->progressionType();
}

TrackProgression::RepeatType Playlist::repeatType()const{
	return m_trackProgression->repeatType();
}

void Playlist::setProgression(TrackProgression::ProgressionType progression, TrackProgression::RepeatType repeat){
	delete m_trackProgression;
	m_trackProgression = TrackProgression::getTrackProgression(&m_songs, progression, repeat);
	sStatusBar->log("Progression type should be altered.");
}

void Playlist::slotRemoveSongs(QList<short int> indices){
	qSort(indices);
	while(indices.size()){
		short int index = indices.takeLast();
		slotRemoveSong(index);
	}
}

void Playlist::slotRemoveSong(short int index){
	delete m_songs.takeAt(index);
	emit songRemoved(index);
}

QString Playlist::progressionText()const{
	return m_trackProgression->progressionText();
}

QString Playlist::repeatText()const{
	return m_trackProgression->repeatText();
}

	